﻿using System.Collections.Generic;

namespace FilteredPageReference
{
    using System;
    using System.Linq;

    /// <summary>
    /// Allowed page types event args
    /// </summary>
    public class AllowedPageTypes
    {

        /// <summary>
        /// Gets the property information.
        /// </summary>
        public PropertyInformation PropertyInformation { get; internal set; }

        /// <summary>
        /// Gets or sets the page type names, should be used if not using page type builder
        /// </summary>
        /// <value>
        /// The page type names.
        /// </value>
        public List<string> PageTypeNames { get; set; }

        /// <summary>
        /// Gets or sets the full name of the type (for use with page type builder)
        /// </summary>
        /// <value>
        /// The full name of the type.
        /// </value>
        public string FullTypeNames { get; set; }

        /// <summary>
        /// Gets or sets the allowed page types and interfaces (for use with page type builder)
        /// </summary>
        /// <value>
        /// The allowed page types and interfaces.
        /// </value>
        public Type[] AllowedPageTypesAndInterfaces
        {
            get
            {
                return FullTypeNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(Type.GetType)
                    .Where(allowedType => allowedType != null).ToArray();
            }
            set
            {
                string typeNames = value.Aggregate(string.Empty, (current, allowedType) => current + string.Concat(allowedType.FullName, ","));
                FullTypeNames = typeNames.TrimEnd(',');
            }
        }

    }
}
